<template>
  <div class="app-container">
    <!--    查询对话框-->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="580px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-col :span="14">
          <el-form-item label-width="125px" label="销售单号" prop="jhmSodh">
            <el-input
              v-model="queryParams.jhmSodh"
              placeholder="请输入销售单号"
              clearable
              size="small"
              style="width: 180px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="状态标志" prop="jhmZtbz">
            <el-select
              v-model="queryParams.jhmZtbz"
              style="width: 142px"
              placeholder="请选择状态标志"
              clearable
              size="small"
            >
              <el-option
                v-for="dict in itmZtbzOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label-width="125px" label="客户编码" prop="somCustwldm">
            <el-input
              v-model="queryParams.somCustwldm"
              placeholder="请输入客户编码"
              clearable
              size="small"
              style="width: 400px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <!--        <el-col :span="24">-->
        <!--          <el-form-item label-width="125px" label="排期日期" prop="dateRange">-->
        <!--            <el-date-picker-->
        <!--              clearable-->
        <!--              size="small"-->
        <!--              v-model="dateRange"-->
        <!--              :picker-options="pickerOptions"-->
        <!--              value-format="yyyy-MM-dd HH:mm:ss"-->
        <!--              type="datetimerange"-->
        <!--              :clearable="false"-->
        <!--              range-separator="至"-->
        <!--              start-placeholder="开始日期"-->
        <!--              end-placeholder="结束日期"-->
        <!--              :default-time="['00:00:00', '23:59:59']"></el-date-picker>-->
        <!--          </el-form-item>-->
        <!--        </el-col>-->
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-check"
          size="mini"
          :disabled="multiple"
          @click="handleCheck"
          v-hasPermi="['marketing:arrangeCheck:check']"
        >
          审核
        </el-button>
        <!-- <el-button type="danger" plain size="mini" :disabled="multiple" @click="handleBack" v-hasPermi="['marketing:arrangeCheck:back']">
          撤回
        </el-button> -->
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['marketing:arrangeCheck:export']"
        >
          导出
        </el-button>
      </el-col>
      <el-col :span="2.2" style="position: absolute; right: 35px; top: 0">
        <Page
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple
        ></Page>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      highlight-current-row
      v-loading="loading"
      :data="arrangeCheckList"
      @selection-change="handleSelectionChange"
      border
      stripe
      ref="arrangeCheckTable"
      @row-click="clickRow"
      :height="tableH"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="排期单号"
        :show-overflow-tooltip="true"
        width="130"
        align="center"
        prop="jhmDjbh"
      />
      <el-table-column
        label="营销单号"
        :show-overflow-tooltip="true"
        width="130"
        align="center"
        prop="jhmSodh"
      />
      <el-table-column
        label="客户"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="somKhdm"
      >
        <template #default="scope">
          <dict-tag :options="customerOptions" :value="scope.row.somKhdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="客户编码"
        :show-overflow-tooltip="true"
        width="120"
        align="center"
        prop="somCustwldm"
      />
      <el-table-column
        label="本厂物料"
        :show-overflow-tooltip="true"
        width="120"
        align="center"
        prop="somWldm"
      />
      <el-table-column
        label="品名规格"
        :show-overflow-tooltip="true"
        width="280"
        prop="somPmgg"
      />
      <el-table-column
        label="订单数量"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="somDdsl"
      />
      <el-table-column
        label="排期数量"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="jhmPqsl"
      />
      <el-table-column
        label="排期日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="jhmPqrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.jhmPqrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="交货日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="somJhrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.somJhrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="建立人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="createBy"
      />
      <el-table-column
        label="建立日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="createTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="审核人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="updateBy"
      />
      <el-table-column
        label="审核日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="updateTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import {
  listArrangeCheck,
  updateJhmztbz,
  exportArrangeCheck,
} from "@/api/marketing/arrangeCheck";
import { getAllCustomer } from "@/api/project/customer";

export default {
  name: "ArrangeCheck",
  data() {
    return {
      // 遮罩层
      loading: true,
      tableH: document.body.clientHeight - 202,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      selectItems: [],
      // 状态标志字典
      itmZtbzOptions: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 交货排期审核表格数据
      arrangeCheckList: [],
      //客户类型
      customerOptions: [],
      // 日期范围
      dateRange: [
        this.$moment().startOf("month").toDate(),
        this.$moment().endOf("month").toDate(),
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openSearch: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: document.body.clientHeight > 900 ? 25 : 18,
        jhmSodh: null,
        somCustwldm: null,
        jhmPqrq: null,
        jhmPqrqEnd: null,
        jhmZtbz: "1",
      },
      // 表单参数
      form: {},
      pickerOptions: {
        shortcuts: [
          {
            text: "未来一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "未来一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              end.setTime(start.getTime() + 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "未来三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              end.setTime(start.getTime() + 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    getAllCustomer().then((response) => {
      for (let i of response.data) {
        i.dictLabel = i.cusName;
        i.dictValue = i.cusCode + "";
        i.listClass = "default";
      }
      this.customerOptions = response.data;
    });
  },
  methods: {
    /** 查询交货排期审核列表 */
    getList() {
      // if (this.dateRange !== null) {
      //   this.queryParams.jhmPqrq = this.moment(this.dateRange[0]).format('YYYY-MM-DD HH:mm:ss')
      //   this.queryParams.jhmPqrqEnd = this.moment(this.dateRange[1]).format('YYYY-MM-DD HH:mm:ss')
      // }
      this.loading = true;
      listArrangeCheck(this.queryParams).then((response) => {
        this.arrangeCheckList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      this.getDicts("sys_audit").then((response) => {
        this.itmZtbzOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.openSearch = false;
    },
    // 表单重置
    reset() {
      this.form = {
        jhmId: null,
        jhmDjbh: null,
        jhmSodh: null,
        jhmPqrq: null,
        jhmPqsl: null,
        jhmJhsl: null,
        jhmZtbz: null,
        jhmDesc: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    clickRow(row) {
      this.$refs.arrangeCheckTable.toggleRowSelection(row);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectItems = selection;
      this.ids = selection.map((item) => item.jhmId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleSearch() {
      this.reset();
      this.title = "查询交货排期审核";
      this.openSearch = true;
    },
    //审核按钮
    handleCheck() {
      for (let i of this.selectItems) {
        if (i.jhmZtbz !== "1") {
          if (i.jhmZtbz === "2") {
            this.msgError("排期单: [" + i.jhmDjbh + "] 已经审核~");
            return;
          } else {
            this.msgError(
              "排期单: [" + i.jhmDjbh + "] 未知状态,请联系系统管理员~"
            );
            return;
          }
        }
      }
      const jhmIds = this.ids;
      const mustZtbz = 1;
      const newZtbz = 2;
      this.$confirm("确认审核选中的单据？", "审核", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return updateJhmztbz(jhmIds, mustZtbz, newZtbz);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("审核成功!");
        })
        .catch(() => {});
    },

    //撤回按钮
    handleBack() {
      for (let i of this.selectItems) {
        if (i.jhmZtbz !== "2") {
          if (i.jhmZtbz === "1") {
            this.msgError("排期单: [" + i.jhmDjbh + "] 未审核,无法撤回~");
            return;
          } else {
            this.msgError(
              "排期单: [" + i.jhmDjbh + "] 未知状态,请联系系统管理员~"
            );
            return;
          }
        }
      }
      const jhmIds = this.ids;
      const mustZtbz = 2;
      const newZtbz = 1;
      this.$confirm("确认撤回选中的单据？", "审核", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return updateJhmztbz(jhmIds, mustZtbz, newZtbz);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("撤回成功!");
        })
        .catch(() => {});
    },
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有交货排期审核数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportArrangeCheck(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="scss" scoped></style>
